<?php
// +----------------------------------------------------------------------
// | zhanshop-cloud / File.php    [ 2025/7/1 10:41 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2025 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\admin\v1\controller;

use app\api\admin\Controller;
use zhanshop\App;
use zhanshop\util\Upload;
use zhanshop\server\Request;
use zhanshop\server\Response;

class Storage extends Controller
{
    /**
     * @api POST uploadVideo 上传文件
     * @apiParam string token 上传token
     * @apiParam string original 源文件内容
     * @apiParam int size 文件大小
     * @apiParam int duration 时长
     * @apiParam file thumbnail= 缩略图
     * @apiParam file upfile 上传文件
     * @apiGroup 存储管理
     */
    public function uploadVideo(Request $request, Response $response)
    {
        $token = $request->param('token');
        $original = $request->param('original');
        $size = $request->param('size');
        $duration = $request->param('duration');
        $request->files('thumbnail');
        $thumbnail = $request->domain().'/v1/storage.file'.str_replace(App::runtimePath(), '', Upload::putFile($request->files('thumbnail'), App::runtimePath().'/file/'.date('Ym')));
        $upfile = $request->domain().'/v1/storage.file'.str_replace(App::runtimePath(), '', Upload::putFile($request->files('upfile'), App::runtimePath().'/file/'.date('Ym')));
        //var_dump($request->rawRequest());
        App::database()->model("system_videos")->insert([
            'thumbnail' => $thumbnail,
            'url' => $upfile,
            'original' => $original,
            'size' => $size,
            'duration' => $duration,
            'create_time' => $request->time(),
            'delete_time' => $request->time(),
        ]);

    }
    /**
     * @api POST files 文件列表
     * @apiGroup 存储管理
     */
    public function files(Request $request, Response $response)
    {

    }
    /**
     * @api GET file/{path} 文件详情
     * @apiGroup 存储管理
     */
    public function file(Request $request, Response $response)
    {
        $request->getData('path');
        $path = explode('/storage.file/', $request->server('request_uri'))[1];
        $absolutePath = App::runtimePath().'/'.$path;
        if(file_exists($absolutePath)){
            $response->rawResponse()->sendfile($absolutePath);
        }else{
            App::error()->setError($path.'不存在', 404);
        }
    }
}